package com.yanqu.road.server.manager.activity.rankunionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.rankunionwar.activity.CrossRankUnionWarActivity;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossRankUnionWarMgr extends TempCrossMgr {

    public final static Map<Integer, CrossRankUnionWarActivity> activityMap = new ConcurrentHashMap<>();

    public static synchronized CrossRankUnionWarActivity getActivity(int activityId) {
        if (activityMap.get(activityId) == null) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null || activityInfo.getType() != eActivityType.RankUnionWar.getValue()) {
                return null;
            }
            CrossRankUnionWarActivity activity = new CrossRankUnionWarActivity(activityInfo);
            activityMap.put(activityId, activity);
        }
        CrossRankUnionWarActivity activity = activityMap.get(activityId);
        return activity;
    }

    public static Map<Integer, CrossRankUnionWarActivity> getActivityMap() {
        return activityMap;
    }

    @Override
    public boolean save() {
        for (CrossRankUnionWarActivity activity : activityMap.values()) {
            activity.save();
        }
        CrossRankUnionWarDebugMgr.logMessageInfo();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.RankUnionWar.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }


    public boolean removeExpireData(){
        List<Integer> removeList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for(int activityId : activityMap.keySet()){
            CrossRankUnionWarActivity activity = activityMap.get(activityId);
            if(activity.getActivity() == null || (nowTime > activity.getActivity().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                removeList.add(activityId);
            }
        }
        for(int activityId : removeList){
            activityMap.remove(activityId);
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
